package com.qfedu.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.entity.RoleEntity;
import com.qfedu.pojo.AuthPojo;
import com.qfedu.pojo.RolePojo;
import com.qfedu.service.AuthService;
import com.qfedu.service.RoleService;
import com.qfedu.util.Ajaxd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private AuthService authService;
//跳转角色添加页面
    @RequestMapping("/RoleAdd.to")
    public String toRoleAdd(){
        return "role/role-add";
    }

    //查询所有角色
    @RequestMapping("/rolelist.do")
    public String toRoleList(RoleEntity roleEntity, HttpServletRequest request){
        PageHelper.startPage(roleEntity.getPageNum(),roleEntity.getPageSize());
        List<RolePojo> rolePojos = roleService.queryRoleList(roleEntity);
        PageInfo<RolePojo> pageInfo=new PageInfo<RolePojo>(rolePojos);
        request.setAttribute("pageinfo",pageInfo);
        return "role/role-list";
    }

//查询所有权限
    @RequestMapping("/authZtree.do")
    @ResponseBody
    public List<AuthPojo> queryAllAuth(){
        List<AuthPojo> authlist = authService.queryAllAuth(null);
        return authlist;
    }
//添加角色
    @RequestMapping("/roleAdd.do")
    @ResponseBody
    public Ajaxd doRoleAdd(RoleEntity roleEntity){
        Ajaxd ajaxd = new Ajaxd();
        Boolean flag = roleService.doAddRole(roleEntity);
        if (flag){
            ajaxd.setFlag(true);
        }else{
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

    //编辑角色
    @RequestMapping("/roleEdit.to")
    public String toRoleEdit(RoleEntity roleEntity,HttpServletRequest request){
        List<RolePojo> rolePojos = roleService.queryauthbyid(roleEntity);
        RolePojo rolePojo = rolePojos.get(0);

        //查询当前用户所拥有的权限
        List<AuthPojo> authPojoList = rolePojo.getAuthPojoList();
        //查询所有权限
        List<AuthPojo> authPojos = authService.queryAllAuth(null);
        for (int i = 0; i < authPojos.size(); i++) {
            for (int j = 0; j < authPojoList.size(); j++) {
                if (authPojos.get(i).getAuid()==authPojoList.get(j).getAuid()){
                    authPojos.get(i).setChecked(true);
                    break;
                }
            }
        }
        //将list集合转换成json字符串
        ObjectMapper jackson = new ObjectMapper();
        String s =null;
        try {
             s = jackson.writeValueAsString(authPojos);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        request.setAttribute("roleinfo",rolePojo);
        request.setAttribute("data",s);
        return "role/role-edit";
    }

    @RequestMapping("/roleEdit.do")
    @ResponseBody
    public Ajaxd doRoleEdit(RoleEntity roleEntity){
        Ajaxd ajaxd = new Ajaxd();
        Boolean flag=false;
        try {

             flag = roleService.editRole(roleEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (flag){
            ajaxd.setFlag(true);
        }else{
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

//删除角色
    @RequestMapping("/deleteRole.do")
    @ResponseBody
    public Ajaxd doDeleteRole(String ridstr){
        Ajaxd ajaxd = new Ajaxd();
        String substring = ridstr.substring(0, ridstr.lastIndexOf(","));
        System.out.println(substring);
        Boolean aBoolean = roleService.deleteRoleById(substring);
        if (aBoolean){
            ajaxd.setFlag(true);
        }else {
            ajaxd.setFlag(false);
        }
        return ajaxd;
    }

}
